//
//  RigisterVC.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/7.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class RigisterVC: MeBaseViewController {
    
    
  
    //用户名
    @IBOutlet weak var tfNum: UITextField!
    
    //密码
    @IBOutlet weak var tfPass: UITextField!
    
    //验证码
    @IBOutlet weak var tfAuto: UITextField!

    //确认密码
    @IBOutlet weak var tfConfirmPass: UITextField!
    
    //同意协议文本
    @IBOutlet weak var agreeLabel: UILabel!
    
    //同意图片显示是否
    @IBOutlet weak var isAgrreIimgShow: UIImageView!
    
    //同意协议
    @IBAction func confirmAgree(_ sender: UIButton) {

        sender.isSelected = !localSave.bool(forKey: agreeLaw)

        if sender.isSelected {
            self.isAgrreIimgShow.isHidden = true
            localSave.set(sender.isSelected, forKey: agreeLaw)
        } else {
            self.isAgrreIimgShow.isHidden = false
            localSave.set(sender.isSelected, forKey: agreeLaw)
        }
        localSave.synchronize()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        isAgrreIimgShow.isHidden = localSave.bool(forKey: agreeLaw)
        
        print(localSave.bool(forKey: agreeLaw))
    }
    
    //发送验证码
    @IBAction func sendAuto(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("发送验证码")))")
        
        //检查验证码是否为空
        if self.tfNum.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfNumIsNull)
        } else if Encryption.checkTelNumber(self.tfNum.text) == false {
            MBManager.showBriefAlert(tfNumNotCor)
        } else if Encryption.checkTelNumber(self.tfNum.text) == true {
            
            let params = ["type":1,
                          "phone":self.tfNum.text!] as [String : Any]
            print(params)
            
            MBManager.showBriefAlert("规范化肩扛萝莉看脚后跟")
            manager.post(sendMsgUrl, parameters: params, success: { (task, response) in
                
                let json = JSON(response:response!)
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(json)
                
                if json["resultmsg"] == "手机号已被注册" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(numUsed)
                    return
                } else if json["resultmsg"] == "成功" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(msgSended)
                }
                
            }) { (task, error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(error)
            }

  
        }
    }
    

    //注册
    @IBAction func rigisterSEL(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("注册")))")
        
        let bool = localSave.bool(forKey: agreeLaw)
        
        
        if self.tfNum.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfNumIsNull)
        } else if self.tfAuto.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfAutoNull)
        } else if self.tfPass.text?.characters.count == 0 {
            MBManager.showBriefAlert(tfPassNull)
        } else if self.tfConfirmPass.text?.characters.count == 0 {
            MBManager.showBriefAlert(confirPassNotNull)
        } else if Encryption.checkTelNumber(self.tfNum.text) == false {
            MBManager.showBriefAlert(tfNumNotCor)
        } else if self.tfPass.text != self.tfConfirmPass.text {
            MBManager.showBriefAlert(passTwoChekc)
        } else if Encryption.checkTelNumber(self.tfNum.text) == true && bool == false {
            
            /**
             注册成功，恭喜您成为一元预购的新成员
             缺少验证码
             短信验证码错误
             手机号已被注册
             缺少手机号参数或手机格式错误或为11位数字
             */
            let params = ["deviceid":deviceID,
                          "devtype": deviceTpye,
                          "username" : self.tfNum.text as Any,
                          "phone":self.tfNum.text as Any,
                          "password":Encryption.md5Encrypt(with:self.tfPass.text) as Any,
                          "code":self.tfAuto.text as Any] as [String : Any]
            MBManager.showBriefMessage("注册中...", in: self.view)
            manager.post(rigisterUrl, parameters: params, success: { (task, response) in
                let json = JSON(response:response!)
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(json)
                
                
                if json["resultmsg"] == "缺少手机号参数或手机格式错误或为11位数字" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(tfNumNotCor)
                } else if json["resultmsg"] == "短信验证码错误" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(authNumWrong)
                } else if json["resultmsg"] == "手机号已被注册"{
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(numUsed)
                    
                } else if json["resultmsg"] == "注册成功，恭喜您成为一元预购的新成员" {
                    MBManager.hideAlert()
                    MBManager.showBriefAlert(rigSuccess)
                }
                
                
            }, failure: { (task, error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(error)
                MBManager.showBriefAlert(netWrong)
            })

        }
        else {
            MBManager.showBriefAlert(agressLawing)
        }

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.agreeLabel.attributedText = CoTool.shareInstance.labelWithUnderLine(text: "同意注册协议")
        self.agreeLabel.isUserInteractionEnabled = true
        
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(RigisterVC.clickLabel))
        self.agreeLabel.addGestureRecognizer(tapGes)

        // Do any additional setup after loading the view.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}

// MARK:- 同意协议
extension RigisterVC {
    func clickLabel() {
        print("点击label")
    }
}

//记录同意图片的显示状态
extension RigisterVC {
    
}
